package com.whc.gulimall.product.app;

import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.product.entity.CategoryBrandRelationEntity;
import com.whc.gulimall.product.service.CategoryBrandRelationService;
import com.whc.gulimall.product.vo.BrandVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌分类关联
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:18:53
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    /**
     * 品牌分类关联 Service
     */
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取品牌关联的分类
     *
     * @param brandId 品牌id
     * @return 统一返回数据
     */
    @GetMapping("/category/list")
    public R categoryList(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> categoryList = categoryBrandRelationService.listCategory(brandId);
        return R.ok().put("data", categoryList);
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId 分类id
     * @return 统一返回数据
     */
    @GetMapping("/brands/list")
    public R brandList(@RequestParam("catId") Long catId) {
        List<BrandVo> entities = categoryBrandRelationService.listBrandsByCatId(catId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveCategoryBrandRelation(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
